$(document).ready(function () {

    $('title').html("登录");
    //获取参数
    var getUrlParameter = function getUrlParameter(sParam) {
        var sPageURL = decodeURIComponent(window.location.search.substring(1)),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;

        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');

            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : sParameterName[1];
            }
        }
    };


    //是否切换到更新页面 1-查看 2-更新 3-添加
    var user = getUrlParameter('user');
    console.log('--- user ---', user);
    if((typeof user === undefined) || user === ''){
        $('#user-name').focus();
    }else{
        $('#pass-tf').focus();
        $('#user-name').val(user);

    }

    var lv = new LoginValidator();
    var lc = new LoginController();

    // main login form //
    $('#login').ajaxForm({
        beforeSubmit: function (formData, jqForm, options) {
            if (lv.validateForm() == false) {
                return false;
            } else {
                // append 'remember-me' option to formData to write local cookie //
                formData.push({name: 'remember-me', value: $('.button-rememember-me-glyph').hasClass('glyphicon-ok')});
                return true;
            }
        },
        // success: function (responseText, status, xhr, $form) {
        success: function (result, xhr, $form) {
            console.log('login result ---- ' + result);
            if (result['id'] == 1) {
                window.location.href = '/home';
            } else {
                generateNotify('', result['msg'], 'danger');
            }
        }
    });

    $('#user-name').focus();

    var ev = new EmailValidator();

    $('#get-credentials-form').ajaxForm({
        url: '/lost-password',
        beforeSubmit: function (formData, jqForm, options) {
            if (ev.validateEmail($('#email-tf').val())) {
                ev.showEmailAlert("<b>稍后!</b> 邮件正在发送...");
                return true;
            } else {
                ev.showEmailAlert("<b>错误!</b> 请输入合法的Email地址");
                return false;
            }
        },
        success: function (responseText, status, xhr, $form) {
            $('#cancel').html('确定');
            $('#retrieve-password-submit').hide();
            ev.showEmailSuccess("请查收邮件，并根据此邮件重置密码");
        },
        error: function (e) {
            if (e.responseText == 'email-not-found') {
                ev.showEmailAlert("该邮箱没有绑定用户. 请确定邮箱地址是否输入正确?");
            } else {
                $('#cancel').html('确定');
                $('#retrieve-password-submit').hide();
                ev.showEmailAlert("抱歉. 出错，请稍后尝试.");
            }
        }
    });



});
